<?php
$db = mysqli_connect("127.0.0.1", "root", "", "kaola");
$type = $_REQUEST["type"];
if($type == "add")
{
  $good_id = $_REQUEST["id"];
  $num = $_REQUEST["num"];
  $price = $_REQUEST["price"];
  $title = $_REQUEST["title"];
  $count = $_REQUEST["count"];
  /* 检查之前是否存在对应的数据，如果存在那么就修改num值，如果不存在那么就插入数据 */
  $sql = "SELECT * FROM car WHERE good_id = '$good_id'";
//   echo $sql;
  $result = mysqli_query($db,$sql);
  if(mysqli_num_rows($result) == 0)
  {
    /* 往数据库表中新增一条数据 */
    $sql = "INSERT INTO `car` (`id`, `good_id`, `num`,`price`,`title`,`count`) VALUES (NULL, $good_id, 1,'$price','$title',$count)";
    // echo $sql;
    $res = mysqli_query($db,$sql);
    echo json_encode(array("status"=>" add success"));
  }else{
    /* 更新数据 */
    $sql = "UPDATE `car` SET `num`= `num`+ 1,`count`=`count`+$price WHERE `good_id`=$good_id";
    $res = mysqli_query($db,$sql);
    echo json_encode(array("status"=>"updata success"));
  }
}elseif($type == "update")
{
  $sign = $_REQUEST["flag"];
  $good_id = $_REQUEST["good_id"];
  if($sign == "plus"){
    $plusSql = "UPDATE `car` SET `num`= `num`+ 1 WHERE `good_id`=$good_id";
    mysqli_query($db,$plusSql);
  }elseif($sign == "reduce"){
    $reduceSql = "UPDATE `car` SET `num`= `num`- 1 WHERE `good_id`=$good_id";
    mysqli_query($db,$reduceSql);
  }
  echo json_encode(array("status"=>"success"), true);
}elseif($type == "del")
{
  $good_id = $_REQUEST["good_id"];
  $delSql = "DELETE FROM `car` WHERE good_id = $good_id";
  mysqli_query($db, $delSql);
  echo json_encode(array("status" => "success"), true);
}
?>